package kr.couponplay.android.support.ad;

import kr.couponplay.android.Constants;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.support.DisplayUtils;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.fsn.cauly.CaulyAdInfo;
import com.fsn.cauly.CaulyAdInfoBuilder;
import com.fsn.cauly.CaulyAdView;
import com.fsn.cauly.CaulyAdViewListener;

public class CaulyAdViewPlatform implements AdPlatform, CaulyAdViewListener {
	private CaulyAdView adViewCauly;
	private int adHeight = LayoutParams.MATCH_PARENT;
	
	
	private void setAdHeight(Context context) {
		int size1 = DisplayUtils.widthPixel(context) / 6;
		int size2 = DisplayUtils.heightPixel(context) / 10;
		
		//가로 6:1, 세로 10:1 중에서 작은 사이즈를 배너 높이로 한다.
		adHeight = (size1 < size2)? size1 : size2;
	}
	

    @Override
    public void onCreateAdView(Activity activity) {
    	setAdHeight(activity.getApplicationContext());
    	
		CaulyAdInfo adInfo = new CaulyAdInfoBuilder("URIyqVbc").effect("RightSlide").build();
		adViewCauly = new CaulyAdView(activity);
		adViewCauly.setAdInfo(adInfo);
		adViewCauly.setAdViewListener(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, adHeight);
		LinearLayout adViewLayout = (LinearLayout) activity.findViewById(R.id.ad_detail_layout);
		adViewLayout.addView(adViewCauly, params);	
        Log.i(Constants.TAG, "카울리 광고 시작");
    }

   	@Override
	public void onReceiveAd(CaulyAdView adView, boolean isChargeableAd) {
		if (isChargeableAd) {
			Log.i(Constants.TAG, "카울리 광고 받음 (height : " + adHeight + ")");
			
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, adHeight);	//LayoutParams.WRAP_CONTENT
			adView.setLayoutParams(params);
			adView.setVisibility(View.VISIBLE);
		} else {
			Log.i(Constants.TAG, "카울리 광고 없음");
			adView.setVisibility(View.GONE);
		}
	}

	@Override
	public void onCloseLandingScreen(CaulyAdView arg0) {
	}

	@Override
	public void onFailedToReceiveAd(CaulyAdView arg0, int arg1, String arg2) {
	}

	@Override
	public void onShowLandingScreen(CaulyAdView arg0) {
	}
	
	@Override
	public void onDestroyAdView() {
		if (adViewCauly != null) {
			adViewCauly.removeAllViews();
			adViewCauly.destroy();
			adViewCauly = null;
		}
	}	
}
